/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;

//import java.beans.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import modelo.Categoria;
import modelo.Subcategoria;

/**
 *
 * @author 2006244090165
 */
public class SubcategoriaDAO {

    public static boolean salvar(Subcategoria subcategorias) {
        boolean result = false;
        try {
            Connection con = Conexao.getConexao();
            PreparedStatement stmt =
                    con.prepareStatement("insert into \n" +
                    "SUBCATEGORIA (ID_CATEGORIA, NOME_SUBCATEGORIA) values (?, ?)");

            Categoria cat = subcategorias.getCategoria();
            stmt.setInt(1, cat.getId_categoria());
            stmt.setString(2, subcategorias.getNome_subcategoria());
            
            stmt.executeUpdate();
            System.out.println("Subcategoria cadastrada com sucesso");
            result= true;
        } catch (SQLException ex) {
            System.err.println(ex);
            result= false;
        } finally {
            System.out.println("Fechando conexao");
            Conexao.desconecta();
        }
          return result;
    }

    public static void editar(Subcategoria subcategorias) {
        try {
            Connection con = Conexao.getConexao();
            PreparedStatement stmt = con.prepareStatement("UPDATE SUBCATEGORIA SET " +
                    "  NOME_SUBCATEGORIA= '" + subcategorias.getNome_subcategoria() + "'" +
                    " WHERE ID_SUBCATEGORIA= " + subcategorias.getId_subcategoria());
            stmt.executeUpdate();
            con.close();
        } catch (SQLException e) {
            System.out.println("Problemas com o BD:");
            e.printStackTrace();
        }
    }
    
    public static Vector getSubcategoriaPorCategoria(int id_categoria) {

        Vector subcategorias = new Vector();
        System.out.println("Listar Subcategoria");
        
        try {
            Connection con = Conexao.getConexao();
            Statement stmt =  con.createStatement();
            String query = ("select * from SUBCATEGORIA  where ID_CATEGORIA =" + id_categoria);
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
               Subcategoria subcategoria = new Subcategoria();
               subcategoria.setId_subcategoria(Integer.parseInt(rs.getString("id_subcategoria")));
               subcategoria.setNome_subcategoria(rs.getString("nome_subcategoria"));
               subcategorias.add(subcategoria);
            }
            con.close();
        } catch (SQLException ex) {
            System.out.println("Problemas com o BD:");
            ex.printStackTrace();
        }
        return subcategorias;
        
    }
    
    //METODO PARA RECUPERAR VARIAS SUBCATEGORIAS DO BANCO
    public static Vector getSubcategorias() {
        
        Vector vp = new Vector();

        try {
            Connection con = Conexao.getConexao();
            Statement stmt =
                    con.createStatement();
            String query = ("select * from SUBCATEGORIA");
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
                Subcategoria subcategoria = new Subcategoria();
                
                subcategoria.setId_subcategoria(Integer.parseInt(rs.getString("ID_SUBCATEGORIA")));
                subcategoria.setNome_subcategoria(rs.getString("NOME_SUBCATEGORIA"));
                
                vp.add(subcategoria);
            }
            Conexao.desconecta();
        } catch (SQLException ex) {
            System.out.println("Problemas com o BD:");
            ex.printStackTrace();
        }
        return vp;
    }

    //exclui através do id
    public static void excluir(int id_subcategoria) {

        try {
            Connection con = Conexao.getConexao();
            java.sql.Statement stmt = con.createStatement();

            String query = "delete from SUBCATEGORIA where ID_SUBCATEGORIA = " + id_subcategoria;

            stmt.executeUpdate(query);

        } catch (SQLException ex) {
            System.err.println(ex);
        } finally {
            Conexao.desconecta();

        }

    }

    //RECUPERA UMA SUBCATEGORIA
    public static Subcategoria getSubcategoria(int id_subcategoria) {

        Subcategoria subcategoria = new Subcategoria();
         System.out.println("Listar Subcategoria");
        
        try {
            Connection con = Conexao.getConexao();
            Statement stmt =  con.createStatement();
            String query = ("select * from SUBCATEGORIA  where ID_SUBCATEGORIA =" + id_subcategoria);
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
               subcategoria.setId_subcategoria(Integer.parseInt(rs.getString("id_subcategoria")));
               subcategoria.setNome_subcategoria(rs.getString("nome_subcategoria"));
               
            }
            con.close();
        } catch (SQLException ex) {
            System.out.println("Problemas com o BD:");
            ex.printStackTrace();
        }
        return subcategoria;
        
    }
}
